﻿// MyNotes - Free Sticky Notes
// Copyright (C) 2011 Alexandre Vicenzi
// 
// This program is free software; you can redistribute it and/or modify it under 
// the terms of the GNU General Public License as published by the Free Software 
// Foundation; either version 3 of the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful, but WITHOUT 
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with 
// this program; if not, write to the Free Software Foundation, Inc., 
// 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MyNotes
{
    public partial class CoolExceptionBox : Form
    {
        private bool showStack = false;
        private Exception ex;

        public CoolExceptionBox(Exception e)
        {
            InitializeComponent();
            if (!(e == null))
            {
                ex = e;
                this.lblError.Text = ex.Message;
                this.btnDetails.Visible = true;
                this.Width = this.lblError.Width + 64;
            }

            if (!showStack)
            {
                this.Height = pnTop.Height + pnButtons.Height + 28;
            }
        }

        private void btnDetails_Click(object sender, EventArgs e)
        {
            if (!showStack)
            {
                this.btnDetails.Text = "&Details <<";
                this.Height = pnTop.Height + pnButtons.Height + pnStack.Height + 28;
                this.pnStack.Visible = true;
                this.showStack = true;
                this.rtbStack.Text = String.Format("Exception class: {0}\n\n", ex.GetType().ToString());
                if (!String.IsNullOrEmpty(ex.StackTrace))
                    this.rtbStack.Text = String.Format("Exception: {0}\n", ex.StackTrace);


                if ((!String.IsNullOrEmpty(ex.StackTrace)) && this.Width < 320)
                    this.Width = this.Width + 140;
                else
                    this.Width = this.Width - 140;
            }
            else
            {
                this.btnDetails.Text = "&Details >>";
                this.pnStack.Visible = false;
                this.Height = pnTop.Height + pnButtons.Height + 28;
                this.showStack = false;
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public static void ShowDialog(Exception e)
        {
            CoolExceptionBox dialog = new CoolExceptionBox(e);
            dialog.ShowDialog();
        }

        public static void ShowDialog(string message)
        {
            Exception e = new Exception(message);
            ShowDialog(e);
        }
    }
}
